package com.qianbao.qianbaobusiness.ui.mine.msgsetting;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.baseandroid.config.Global;
import com.baseandroid.mvp.BaseMvpActivity;
import com.kyleduo.switchbutton.SwitchButton;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.ui.widget.dialog.ConfirmDialog;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;

public class MessageSettingActivity extends BaseMvpActivity<MessageSettingView, MessageSettingPresenter> implements MessageSettingView, CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.sb_push_all)
    SwitchButton mSbPushAll;
    @BindView(R.id.sb_bill_push)
    SwitchButton mSbBillPush;
    @BindView(R.id.fl_bill_push)
    FrameLayout mFlBillPush;
    @BindView(R.id.sb_bill_voice)
    SwitchButton mSbBillVoice;
    @BindView(R.id.fl_bill_voice)
    FrameLayout mFlBillVoice;

    private ConfirmDialog mConfirmDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_message_setting;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        mTvTitle.setText("设置");
        if (Global.getPushOpenedState()) {
            mSbPushAll.setChecked(true);
            mSbBillPush.setEnabled(true);
            mSbBillVoice.setEnabled(true);
            mFlBillPush.setVisibility(View.VISIBLE);
            mFlBillVoice.setVisibility(View.VISIBLE);
        } else {
            mSbPushAll.setChecked(false);
            mSbBillPush.setChecked(false);
            mSbBillVoice.setChecked(false);
            mSbBillPush.setEnabled(false);
            mSbBillVoice.setEnabled(false);
            mFlBillPush.setVisibility(View.GONE);
            mFlBillVoice.setVisibility(View.GONE);
        }

        if (Global.getPushOpenedState() && Global.getPushBillState()) {
            mSbBillPush.setChecked(true);
        } else {
            mSbBillPush.setChecked(false);
        }
        if (Global.getPushOpenedState() && Global.getPushVoiceState()) {
            mSbBillVoice.setChecked(true);
        } else {
            mSbBillVoice.setChecked(false);
        }

        mSbPushAll.setOnCheckedChangeListener(this);
        mSbBillPush.setOnCheckedChangeListener(this);
        mSbBillVoice.setOnCheckedChangeListener(this);
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick(R.id.iv_back_toolbar)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (buttonView.equals(mSbPushAll)) {
            if (isChecked) {
                if (!TextUtils.isEmpty(Global.getPushConfig()) && Global.getPushConfig()
                        .equals("1")) {
                    PushServiceFactory.getCloudPushService()
                            .turnOnPushChannel(new CommonCallback() {
                                @Override
                                public void onSuccess(String s) {
                                    Log.e("turnOnPushChannel", "turnOnPushChannel success");
                                }

                                @Override
                                public void onFailed(String s, String s1) {
                                    Log.e("turnOnPushChannel", "init turnOnPushChannel onFailed");
                                }
                            });
                } else {
                    JPushInterface.resumePush(Global.getContext());
                }
                Global.setPushOpenedState(true);
                mFlBillPush.setVisibility(View.VISIBLE);
                mFlBillVoice.setVisibility(View.VISIBLE);
                mSbBillPush.setEnabled(true);
                mSbBillVoice.setEnabled(true);
            } else {
                mConfirmDialog = new ConfirmDialog.Builder(this).setContentText("关闭后，将无法正常接收语音播报等通知，确认关闭？")
                        .setCanceledOnTouchOutside(false)
                        .setConfirmListener(new ConfirmDialog.Builder.ConfirmClickListener() {
                            @Override
                            public void click(boolean b) {
                                mConfirmDialog.dismiss();
                                if (b) {
                                    if (!TextUtils.isEmpty(Global.getPushConfig()) && Global
                                            .getPushConfig()
                                            .equals("1")) {
                                        PushServiceFactory.getCloudPushService()
                                                .turnOffPushChannel(new CommonCallback() {
                                                    @Override
                                                    public void onSuccess(String s) {
                                                        Log.e("turnOffPushChannel", "turnOnPushChannel success");
                                                    }

                                                    @Override
                                                    public void onFailed(String s, String s1) {
                                                        Log.e("turnOffPushChannel", "turnOnPushChannel onFailed");
                                                    }
                                                });
                                    } else {
                                        JPushInterface.stopPush(Global.getContext());
                                    }
                                    Global.setPushOpenedState(false);
                                    Global.setPushBillState(false);
                                    Global.setPushVoiceState(false);
                                    mFlBillPush.setVisibility(View.GONE);
                                    mFlBillVoice.setVisibility(View.GONE);
                                    mSbPushAll.setChecked(false);
                                    mSbBillPush.setChecked(false);
                                    mSbBillVoice.setChecked(false);
                                    mSbBillPush.setEnabled(false);
                                    mSbBillVoice.setEnabled(false);
                                } else {
                                    Global.setPushOpenedState(true);
                                    mFlBillPush.setVisibility(View.VISIBLE);
                                    mFlBillVoice.setVisibility(View.VISIBLE);
                                    mSbPushAll.setChecked(true);
                                    mSbBillPush.setEnabled(true);
                                    mSbBillVoice.setEnabled(true);
                                }
                            }
                        })
                        .create();
                mConfirmDialog.show();

            }
        } else if (buttonView.equals(mSbBillPush)) {
            if (isChecked) {
                Global.setPushBillState(true);
            } else {
                Global.setPushBillState(false);
            }
        } else if (buttonView.equals(mSbBillVoice)) {
            if (isChecked) {
                Global.setPushVoiceState(true);
            } else {
                Global.setPushVoiceState(false);
            }
        }
    }
}
